package com.huaxia.controller.performance;

import com.huaxia.pojo.performance.BranchPerformance;
import com.huaxia.pojo.performance.BranchLongPerformance;
import com.huaxia.pojo.performance.BranchReach;
import com.huaxia.pojo.performance.SaleDeptPerformance;
import com.huaxia.pojo.performance.SaleDeptLongPerformance;
import com.huaxia.pojo.performance.SaleDeptReach;
import com.huaxia.pojo.performance.*;
import com.huaxia.service.performance.PerformanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author wangli 爱我华夏，爱我中华！
 * @date 2020/06/04 16:06
 */
@RestController
@RequestMapping("/performance")
public class PerformanceController {

    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private PerformanceService performanceService;

    //获取中支数据本月
    @GetMapping("/getBranchPerformanceThisMon")
    public List<BranchPerformance> getBranchPerformanceThisMon(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchPerformance> branchPerformanceThisMon = (List<BranchPerformance>) servletContext.getAttribute("branchPerformanceThisMon");
        if(branchPerformanceThisMon==null){
            branchPerformanceThisMon = performanceService.getBranchPerformanceThisMon();
            servletContext.setAttribute("branchPerformanceThisMon",branchPerformanceThisMon);
            return branchPerformanceThisMon;
        }
        return branchPerformanceThisMon;
    }

    //获取中支数据下月
    @GetMapping("/getBranchPerformanceNextMon")
    public List<BranchPerformance> getBranchPerformanceNextMon(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchPerformance> branchPerformanceNextMon = (List<BranchPerformance>) servletContext.getAttribute("branchPerformanceNextMon");
        if(branchPerformanceNextMon==null){
            branchPerformanceNextMon = performanceService.getBranchPerformanceNextMon();
            servletContext.setAttribute("branchPerformanceNextMon",branchPerformanceNextMon);
            return branchPerformanceNextMon;
        }
        return branchPerformanceNextMon;
    }

    //获取中支长期数据下月
    @GetMapping("/getBranchLongPerformanceNextMon")
    public List<BranchLongPerformance> getBranchLongPerformanceNextMon(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchLongPerformance> branchLongPerformanceNextMon = (List<BranchLongPerformance>) servletContext.getAttribute("branchLongPerformanceNextMon");
        if(branchLongPerformanceNextMon==null){
            branchLongPerformanceNextMon = performanceService.getBranchLongPerformanceNextMon();
            servletContext.setAttribute("branchLongPerformanceNextMon",branchLongPerformanceNextMon);
            return branchLongPerformanceNextMon;
        }
        return branchLongPerformanceNextMon;
    }


    //获取中支期交数据本月
    @GetMapping("/getBranchLongPerformanceThisMon")
    public List<BranchLongPerformance> getBranchLongPerformanceThisMon(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchLongPerformance> branchLongPerformanceThisMon = (List<BranchLongPerformance>) servletContext.getAttribute("branchLongPerformanceThisMon");
        if(branchLongPerformanceThisMon==null){
            branchLongPerformanceThisMon = performanceService.getBranchLongPerformanceThisMon();
            servletContext.setAttribute("branchLongPerformanceThisMon",branchLongPerformanceThisMon);
            return branchLongPerformanceThisMon;
        }
        return branchLongPerformanceThisMon;
    }



    @GetMapping("/findYxTargetThisMon")
    public Boolean findYxTargetThisMon(){
        return performanceService.findYxTargetThisMon();
    }

    @GetMapping("/findYxTargetNextMon")
    public Boolean findYxTargetNextMon(){
        return performanceService.findYxTargetNextMon();
    }

    //获取营服数据
    @GetMapping("/getSaleDeptPerformance")
    public List<SaleDeptPerformance> getSaleDeptPerformance(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptPerformance>  saleDeptPerformances = (List<SaleDeptPerformance>) servletContext.getAttribute("saleDeptPerformances");
        if(saleDeptPerformances==null){
            saleDeptPerformances = performanceService.getSaleDeptPerformance();
            servletContext.setAttribute("saleDeptPerformances",saleDeptPerformances);
            return saleDeptPerformances;
        }
        return saleDeptPerformances;
    }

    //获取营服期交数据
    @GetMapping("/getSaleDeptLongPerformance")
    public List<SaleDeptLongPerformance> getSaleDeptLongPerformance(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptLongPerformance>  saleDeptLongPerformances = (List<SaleDeptLongPerformance>) servletContext.getAttribute("saleDeptLongPerformances");
        if(saleDeptLongPerformances==null){
            saleDeptLongPerformances = performanceService.getSaleDeptLongPerformance();
            servletContext.setAttribute("saleDeptLongPerformances",saleDeptLongPerformances);
            return saleDeptLongPerformances;
        }
        return saleDeptLongPerformances;
    }

    //获取收展数据
    @GetMapping("/getSzPerformance")
    public List<SzPerformance> getSzPerformance(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SzPerformance>  szPerformances = (List<SzPerformance>) servletContext.getAttribute("szPerformances");
        if(szPerformances==null){
            szPerformances = performanceService.getSzPerformance();
            servletContext.setAttribute("szPerformances",szPerformances);
            return szPerformances;
        }
        return szPerformances;
    }

    //获取收展期交数据
    @GetMapping("/getSzLongPerformance")
    public List<SzLongPerformance> getSzLongPerformance(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SzLongPerformance>  szLongPerformances = (List<SzLongPerformance>) servletContext.getAttribute("szLongPerformances");
        if(szLongPerformances==null){
            szLongPerformances = performanceService.getSzLongPerformance();
            servletContext.setAttribute("szLongPerformances",szLongPerformances);
            return szLongPerformances;
        }
        return szLongPerformances;
    }

    //获取中支数据
    @GetMapping("/getBranchReach")
    public List<BranchReach> getBranchReach(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchReach> branchReachs = (List<BranchReach>) servletContext.getAttribute("branchReachs");
        if(branchReachs==null){
            branchReachs = performanceService.getBranchReach();
            servletContext.setAttribute("branchReachs",branchReachs);
            return branchReachs;
        }
        return branchReachs;
    }


    //获取营服数据
    @GetMapping("/getSaleDeptReach")
    public List<SaleDeptReach> getSaleDeptReach(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptReach>  saleDeptReachs = (List<SaleDeptReach>) servletContext.getAttribute("saleDeptReachs");
        if(saleDeptReachs==null){
            saleDeptReachs = performanceService.getSaleDeptReach();
            servletContext.setAttribute("saleDeptReachs",saleDeptReachs);
            return saleDeptReachs;
        }
        return saleDeptReachs;
    }


}
